package com.atguigu.rabbitmq.five;

import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/*
* 消息的接收,也就是消费者
* 演示fanout交换机
* */
public class ReceiveLogs02 {

    /*交换机的名字*/
    public static final String EXCHANGE_NAME="logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();
        /*声明一个交换机*/
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        /*声明一个队列，临时队列，队列的名称是随机的
        *当消费者断开与队列的连接的时候，队列就自动删除了
        * */
        String queueName = channel.queueDeclare().getQueue();
        /*将交换机与队列进行捆绑*/
        /*队列名称，交换机名称，routingkey*/
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        System.out.println("等待接收消息，把接收到的消息打印在屏幕上面......");

        DeliverCallback deliverCallback=(consumerTag,message)->{
            System.out.println("ReceiveLogs02控制台打印接收到的消息："+new String(message.getBody(),"UTF-8"));
        };

        CancelCallback cancelCallback =consumerTag->{
            System.out.println("取消消息的接收");
        };

        /*接收消息*/
        /*队列名，是否自动确认接收，接收消息回调，取消接收消息回调*/
        channel.basicConsume(queueName, true, deliverCallback, cancelCallback);

    }

}









